package com.geese.module.wechat.controller;

import com.geese.common.BaseController;
import com.geese.common.entity.Business;
import com.geese.common.entity.Courier;
import com.geese.common.entity.Order;
import com.geese.common.util.DateUtil;
import com.geese.common.util.TextValidator;
import com.geese.exception.BusinessException;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import com.geese.module.wechat.service.IShopKeeperService;
import com.geese.module.wechat.vo.ShopkeeperOrderVO;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信商家控制器
 * Created by huweiwei on 2017/3/9.
 */
@RestController
@RequestMapping("/wechat/shopkeeper")
@Scope("prototype")
public class ShopkeeperController extends BaseController {

    @Autowired
    private IShopKeeperService shopKeeperService;
    @Autowired
    private UserService userService;

    /**
     * 派单
     * @param imageUrl
     * @return
     */
    @PostMapping("/push")
    public String push (@RequestParam String imageUrl, @RequestParam String token) {
        if (StringUtils.isBlank(imageUrl)) {
            throw new BusinessException(120059);
        }
        SecurityUser securityUser = userService.getLoginUser(token);
        Order order = shopKeeperService.doPushOrder(securityUser.getUserId(), imageUrl);
        Map<String, Object> result = new HashMap<>();
        result.put("orderCode", order.getCode());
        result.put("orderId", order.getId());
        return response(result);
    }

    /**
     * 商家个人中心
     * @return
     */
    @GetMapping("/info")
    public String info(@RequestParam String token) {
        SecurityUser securityUser = userService.getLoginUser(token);
        Map<String, Object> result = shopKeeperService.getShopkeeperInfo(securityUser.getUserId());
        return response(result);
    }

    /**
     * 订单列表
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param currPage  查询页码
     * @param pageSize  分页大小
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam (required = false) String startDate,
                       @RequestParam (required = false) String endDate,
                       @RequestParam (required = false) Integer currPage,
                       @RequestParam (required = false) Integer pageSize,
                       @RequestParam String token
    ) {
        SecurityUser securityUser = userService.getLoginUser(token);
        //封装查询参数
        ShopkeeperOrderVO shopkeeperOrderVO = new ShopkeeperOrderVO();
        shopkeeperOrderVO.setCurrPage(currPage == null || currPage <= 0 ? 1 : currPage);
        shopkeeperOrderVO.setPageSize(pageSize == null || pageSize <= 0 ? 15 : pageSize);
        if (startDate != null) {
            shopkeeperOrderVO.setStartDate(DateUtil.parseDate(startDate, DateUtil.PATTERN_ISO_ON_DATE));
        }
        if (endDate != null) {
            Date tempDate = DateUtil.parseDate(endDate, DateUtil.PATTERN_ISO_ON_DATE);
            shopkeeperOrderVO.setEndDate(DateUtil.addDays(tempDate, 1));
        }
        Map<String, Object> result = shopKeeperService.getOrderList(shopkeeperOrderVO, securityUser);
        return response(result);
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/details")
    public String details(@RequestParam int orderId, @RequestParam String token) {
        userService.checkBusinessUser(token);
        if (orderId <= 0) {
            throw new BusinessException(120058);
        }
        Map<String, Object> result = shopKeeperService.getOrderDetails(orderId);
        return response(result);
    }

    /**
     * 催单
     * @param orderId
     * @return
     */
    @PostMapping("/remind")
    public String remind(@RequestParam int orderId, @RequestParam String token) {
        userService.checkBusinessUser(token);
        if (orderId <= 0) {
            throw new BusinessException(120058);
        }
        shopKeeperService.doRemind(orderId);
        return response();
    }

    /**
     * 商家注册接口
     * @param business 商家对象接收数据
     * @param password 密码
     * @return
     */
    @PostMapping("/register")
    public String register(Business business, @RequestParam String password) {

        if (StringUtils.isBlank(password)) {
            throw new BusinessException(120060);
        } else if (StringUtils.isBlank(business.getName())) {
            throw new BusinessException(120021);
        } else if (StringUtils.isBlank(business.getLicenseUrl())) {
            throw new BusinessException(120022);
        } else if (StringUtils.isBlank(business.getMobile()) || !TextValidator.isMobileExact(business.getMobile())) {
            throw new BusinessException(120023);
        } else if (StringUtils.isBlank(business.getProvince())) {
            throw new BusinessException(120024);
        } else if (StringUtils.isBlank(business.getCity())) {
            throw new BusinessException(120025);
        } else if (StringUtils.isBlank(business.getArea())) {
            throw new BusinessException(120026);
        } else if (StringUtils.isBlank(business.getAddress())) {
            throw new BusinessException(120027);
        }
//        else if (business.getSettleCycle() == null || business.getSettleCycle() <= 0) {
//            throw new BusinessException(120028);
//        } else if (business.getSingleAmount() == null || business.getSingleAmount() <= 0) {
//            throw new BusinessException(120029);
//        }
        shopKeeperService.doRegister(business, password);
        //不抛异常即注册成功
        return response();
    }
}
